package com.raccoonagents.tools.cascet2d.model;

import com.raccoonagents.tools.cascet2d.interfaces.IRenderableStageElement;
import com.raccoonagents.tools.cascet2d.model.components.AbstractStageComponent;
import com.raccoonagents.tools.cascet2d.util.FontUtils;
/**
 * A renderable text class.
 * @author Kari Kuvaja
 *
 */

public class Text extends AbstractStageComponent implements IRenderableStageElement{
	/** the content of this text*/
	private final StringBuffer content;
	/** opacity 0.0f - 1.0f */
	private float opacity;
	
	/** the text color */
	protected float [] color = new float []{1.0f,1.0f,1.0f};
	/**
	 * 
	 * @return the opacity value for this text.
	 */
	public float getOpacity() {
		return opacity;
	}
	/**
	 * sets the opacity value for this text for rendering purposes
	 * @param opacity the desired amount of opacity
	 */
	public void setOpacity(final float opacity) {
		this.opacity = opacity;
	}
	/**
	 * 
	 * @return the rendering color of this text
	 */
	public float[] getColor() {
		return color;
	}
	/**
	 * 
	 * @param color the desired rendering color for this text
	 */
	public void setColor(final float[] color) {
		this.color = color;
	}
	/**
	 * 
	 * @param text the new text for this instance
	 */
	public void setText(final String text){
		this.content.replace(0, content.length(),"");
		content.trimToSize();
		content.append(text);
	}
	/**
	 * 
	 * @return the content of this text as String
	 */
	public String getText(){
		return content.toString();
	}

	/**
	 * 
	 * @return the length of the content buffer.
	 */
	public int getLength(){
		return content.length();
	}
	/**
	 * 
	 * @return the dimensions of the font used for this text
	 */
	public Vector2f getFontSize(){
		return FontUtils.dimensions;
	}
	/**
	 * appends the content of this text
	 * @param str the string to be appended
	 */
	public void append(final String str){
		content.append(str);
	}
	/**
	 * 
	 * @return the rows of this text as an array. \n is used to split rows.
	 */
	public String [] getRows(){
		return content.toString().split("\n");
	}
	
	public Text(final String text){
		content = new StringBuffer();
		content.append(text);
		this.opacity = 1.0f;
		this.color = new float []{1.0f,1.0f,1.0f};
	}


	/**
	 * 
	 * @return the rendering size of this text at screen.
	 */

	@Override
	public Vector2f getSize() {
		final String [] rows = getRows();
		int max = 0;
		for(final String row : rows){
			if(row.length() > max){
				max = row.length();
			}
		}
		
		size = new Vector2f(max * getFontSize().x, rows.length * getFontSize().y);
		return size; 		
	}

	@Override
	public void render() {
		FontUtils.renderText(this);
	}

	@Override
	public Vector2f getLocation() {
		return localLocation;
	}


}
